<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Model\QuicklyGrab;


use App\Model\Base;
use Illuminate\Support\Facades\DB;

class QuicklyGrabList extends Base
{

    protected $table = 't_quickly_grab_info';

    protected $appends = ['surplus_win_count','surplus_fail_count','win_count','fail_count','code_count'];

    public function getSurplusWinCountAttribute()
    {
        return DB::table('t_discount_info')->where('match_id',$this->id)->where('reward_type',1)->where('status',0)->count('id');
    }

    public function getSurplusFailCountAttribute()
    {
        return DB::table('t_discount_info')->where('match_id',$this->id)->where('reward_type',2)->where('status',0)->count('id');
    }

    public function getWinCountAttribute()
    {
        return DB::table('t_discount_info')->where('match_id',$this->id)->where('reward_type',1)->count('id');
    }

    public function getFailCountAttribute()
    {
        return DB::table('t_discount_info')->where('match_id',$this->id)->where('reward_type',2)->count('id');
    }

    public function getCodeCountAttribute()
    {
        return DB::table('t_discount_info')->where('match_id',$this->id)->count('id');
    }

    public function getIconUrlAttribute($value)
    {
        return env('IMAGE_URL') . $value;
    }

    public function getBigIconUrlAttribute($value)
    {
        return env('IMAGE_URL') . $value;
    }

    public function getStatusAttribute($value)
    {
        $date = date('Y-m-d H:i:s');
        if ($date > $this->start_time && $date < $this->end_time) {
            return $value;
        }
        return 1;
    }
}